import React, {
    Component,
    PropTypes,
} from 'react';

import {
    requireNativeComponent,
    NativeModules,
    DeviceEventEmitter,
    Platform
} from 'react-native';

const _module = NativeModules.BaiduLocationService;

type GeoOptions = {
    timeout: number;
maximumAge: number;
enableHighAccuracy: bool;
distanceFilter: number;
}

var subscriptions = [];

var updatesEnabled = false;

export default {
    ///获取当前ＧPS位置
    getCurrentPosition: function (
        geo_success: Function,
        geo_error?: Function,
    geo_options?: GeoOptions
) {
    _module.getCurrentPosition(
        geo_options || {},
        geo_success,
        geo_error || console.error
    );
},

getCurrentPosition2() {
    if(Platform.OS === 'android')
        _module.getCurrentPosition2();

},

/*
* Invokes the success callback whenever the location changes.  Supported
* options: timeout (ms), maximumAge (ms), enableHighAccuracy (bool), distanceFilter(m)
*/
watchPosition: function (success: Function, error?: Function, options?: GeoOptions): number {
    if (!updatesEnabled) {
        _module.startObserving(options || {});
        updatesEnabled = true;
    }
    var watchID = subscriptions.length;
    subscriptions.push([
        DeviceEventEmitter.addListener(
            'baiduLocationDidChange',
            success
        ),
        error ? DeviceEventEmitter.addListener(
            'baiduLocationError',
            error
        ) : null,
    ]);
    return watchID;
},

clearWatch: function (watchID: number) {
    var sub = subscriptions[watchID];
    if (!sub) {
        // Silently exit when the watchID is invalid or already cleared
        // This is consistent with timers
        return;
    }

    sub[0].remove();
    // array element refinements not yet enabled in Flow
    var sub1 = sub[1]; sub1 && sub1.remove();
    subscriptions[watchID] = undefined;
    var noWatchers = true;
    for (var ii = 0; ii < subscriptions.length; ii++) {
        if (subscriptions[ii]) {
            noWatchers = false; // still valid subscriptions
        }
    }
    if (noWatchers) {
        _module.stopObserving();
    }
},

stopObserving: function () {
    if (updatesEnabled) {
        _module.stopObserving();
        updatesEnabled = false;
        for (var ii = 0; ii < subscriptions.length; ii++) {
            var sub = subscriptions[ii];
            if (sub) {
                sub[0].remove();
                // array element refinements not yet enabled in Flow
                var sub1 = sub[1]; sub1 && sub1.remove();
            }
        }
        subscriptions = [];
    }
}
}

